package com.heima.behavior.service.impl;

import com.alibaba.fastjson.JSON;
import com.heima.behavior.service.ApBehaviorEntryService;
import com.heima.behavior.service.ApReadBehaviorService;
import com.heima.common.exception.CustException;
import com.heima.model.behavior.dtos.ReadBehaviorDTO;
import com.heima.model.behavior.pojos.ApBehaviorEntry;
import com.heima.model.behavior.pojos.ApLikesBehavior;
import com.heima.model.behavior.pojos.ApReadBehavior;
import com.heima.model.common.constants.HotArticleConstants;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.mess.app.NewBehaviorDTO;
import com.heima.model.threadlocal.AppThreadLocalUtils;
import com.heima.model.user.pojos.ApUser;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.util.Date;
@Service
@Slf4j
public class ApReadBehaviorServiceImpl implements ApReadBehaviorService {

    @Autowired
    ApBehaviorEntryService apBehaviorEntryService;
    @Autowired
    MongoTemplate mongoTemplate;
    @Autowired
    RabbitTemplate rabbitTemplate;
    /**
     * 用户阅读行为，点击文章阅读量+1，查看时长
     * @param dto
     * @return
     */
    @Override
    public ResponseResult readBehavior(ReadBehaviorDTO dto) {
        /**
         * 1. 校验参数 文章id必须传
         * 2. 根据登录用户id 或 设备id查询行为实体数据(阅读操作可不登录)
         * 3. 判断阅读行为是否存在
         * 4. 存在  将阅读行为的count字段加1 并修改
         * 5. 不存在  创建阅读行为 并初始化count字段值为 1
         */
        if (dto.getArticleId() == null) {
            CustException.cust(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        ApUser user = AppThreadLocalUtils.getUser();
        ApBehaviorEntry apBehaviorEntry = apBehaviorEntryService.findByUserIdOrEquipmentId(user.getId(), dto.getEquipmentId());
        if (apBehaviorEntry == null) {
            CustException.cust(AppHttpCodeEnum.PARAM_REQUIRE,"实体参数不存在");
        }
        //根据实体类id和文章id查询阅读行为是否存在
        Query query = Query.query(Criteria.where("entryId").is(apBehaviorEntry.getId()).and("articleId").is(dto.getArticleId()));
        ApReadBehavior readBehavior = mongoTemplate.findOne(query, ApReadBehavior.class);
        //不存在save
        if (readBehavior == null) {
            ApReadBehavior apReadBehavior = new ApReadBehavior();
            apReadBehavior.setCreatedTime(new Date());
            apReadBehavior.setArticleId(dto.getArticleId());
            apReadBehavior.setEntryId(apBehaviorEntry.getId());
            apReadBehavior.setCount((short)1);
            apReadBehavior.setUpdatedTime(new Date());
            ApReadBehavior save = mongoTemplate.save(apReadBehavior);
            System.out.println("save = " + save);

        } else {
            //存在修改
            Query query2= Query.query(Criteria.where("_id").is(readBehavior.getId()));
            mongoTemplate.remove(query2,ApReadBehavior.class);
            readBehavior.setCount((short) (readBehavior.getCount()+1));
            readBehavior.setUpdatedTime(new Date());
            ApReadBehavior save = mongoTemplate.save(readBehavior);
            System.out.println("save = " + save);

        }

        NewBehaviorDTO mess = new NewBehaviorDTO();
        mess.setType(NewBehaviorDTO.BehaviorType.VIEWS);
        mess.setArticleId(dto.getArticleId());
        mess.setAdd(1);
        rabbitTemplate.convertAndSend(HotArticleConstants.HOT_ARTICLE_SCORE_BEHAVIOR_QUEUE, JSON.toJSONString(mess));


        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
